package com.kxmall.huishou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kxmall.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kxmall.huishou.domain.bo.HsWasteNewsBo;
import com.kxmall.huishou.domain.vo.HsWasteNewsVo;
import com.kxmall.huishou.domain.HsWasteNews;
import com.kxmall.huishou.mapper.HsWasteNewsMapper;
import com.kxmall.huishou.service.IHsWasteNewsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 环保科普信息Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-23
 */
@RequiredArgsConstructor
@Service
public class HsWasteNewsServiceImpl implements IHsWasteNewsService {

    private final HsWasteNewsMapper baseMapper;

    /**
     * 查询环保科普信息
     */
    @Override
    public HsWasteNewsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询环保科普信息列表
     */
    @Override
    public TableDataInfo<HsWasteNewsVo> queryPageList(HsWasteNewsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsWasteNews> lqw = buildQueryWrapper(bo);
        Page<HsWasteNewsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询环保科普信息列表
     */
    @Override
    public List<HsWasteNewsVo> queryList(HsWasteNewsBo bo) {
        LambdaQueryWrapper<HsWasteNews> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsWasteNews> buildQueryWrapper(HsWasteNewsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsWasteNews> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), HsWasteNews::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), HsWasteNews::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getSource()), HsWasteNews::getSource, bo.getSource());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthor()), HsWasteNews::getAuthor, bo.getAuthor());
        return lqw;
    }

    /**
     * 新增环保科普信息
     */
    @Override
    public Boolean insertByBo(HsWasteNewsBo bo) {
        HsWasteNews add = BeanUtil.toBean(bo, HsWasteNews.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改环保科普信息
     */
    @Override
    public Boolean updateByBo(HsWasteNewsBo bo) {
        HsWasteNews update = BeanUtil.toBean(bo, HsWasteNews.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsWasteNews entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除环保科普信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
//        HsWasteNews hsWasteNews = new HsWasteNews();
//        hsWasteNews.setDelFlag(1l);
//        int num = baseMapper.update(hsWasteNews, new QueryWrapper<HsWasteNews>().in("id", ids));
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
